/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZDataSource;
/*     */ import com.zimbra.client.ZFolder;
/*     */ import com.zimbra.client.ZImapDataSource;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Log.Level;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailclient.MailConfig.Security;
/*     */ import com.zimbra.cs.mailclient.imap.ImapConfig;
/*     */ import com.zimbra.cs.mailclient.imap.ImapConnection;
/*     */ import com.zimbra.cs.mailclient.imap.ListData;
/*     */ import com.zimbra.cs.mailclient.imap.MailboxInfo;
/*     */ import com.zimbra.soap.type.DataSource.ConnectionType;
/*     */ import java.io.IOException;
/*     */ import java.util.List;
/*     */ import org.junit.After;
/*     */ import org.junit.Assert;
/*     */ import org.junit.Before;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestImapSync
/*     */ {
/*     */   private static final String LOCAL_USER = "TestImapSync";
/*     */   private static final String TEST_FOLDER_1 = "TestOne";
/*  40 */   private static final Log LOG = ZimbraLog.test;
/*     */   
/*     */   private final ImapConfig config;
/*     */   private final String pass;
/*  44 */   private final ImapFolder imapFolder1 = new ImapFolder("TestOne");
/*     */   private ZMailbox localMailbox;
/*     */   private ImapConnection imapConnection;
/*     */   private ZDataSource dataSource;
/*     */   
/*     */   public TestImapSync(ImapConfig config, String pass) {
/*  50 */     this.config = config;
/*  51 */     this.pass = pass;
/*  52 */     config.getLogger().setLevel(Log.Level.trace);
/*     */   }
/*     */   
/*     */   public TestImapSync() {
/*  56 */     this.config = new ImapConfig();
/*  57 */     this.config.setHost("localhost");
/*  58 */     this.config.setPort(7143);
/*  59 */     this.config.setAuthenticationId("user1");
/*  60 */     this.pass = "test123";
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testSync() throws Exception {
/*  65 */     LOG.info("Testing adding message to local mailbox");
/*  66 */     ZFolder folder = TestUtil.createFolder(this.localMailbox, "TestOne");
/*  67 */     TestUtil.addMessage(this.localMailbox, "msg1", folder.getId(), "u");
/*  68 */     Assert.assertFalse(this.imapFolder1.exists());
/*  69 */     syncFolders();
/*  70 */     Assert.assertTrue(this.imapFolder1.exists());
/*  71 */     MailboxInfo mb = this.imapFolder1.select();
/*  72 */     Assert.assertEquals(1L, mb.getExists());
/*  73 */     Assert.assertEquals(1L, mb.getUnseen());
/*     */   }
/*     */   
/*     */   @Before
/*     */   public void setUp() throws Exception {
/*  78 */     if (TestUtil.accountExists("TestImapSync")) {
/*  79 */       TestUtil.deleteAccount("TestImapSync");
/*     */     }
/*  81 */     TestUtil.createAccount("TestImapSync");
/*  82 */     this.localMailbox = TestUtil.getZMailbox("TestImapSync");
/*  83 */     this.dataSource = createDataSource();
/*  84 */     connect();
/*  85 */     deleteImapFolders();
/*     */   }
/*     */   
/*     */   @After
/*     */   public void tearDown() throws Exception {
/*  90 */     if (TestUtil.accountExists("TestImapSync")) {
/*  91 */       TestUtil.deleteAccount("TestImapSync");
/*     */     }
/*  93 */     if (this.imapConnection != null) {
/*  94 */       deleteImapFolders();
/*  95 */       this.imapConnection.logout();
/*  96 */       this.imapConnection = null;
/*     */     }
/*  98 */     this.localMailbox = null;
/*  99 */     this.dataSource = null;
/*     */   }
/*     */   
/*     */   private void syncFolders() throws Exception {
/* 103 */     TestUtil.importDataSource(this.dataSource, this.localMailbox, null);
/*     */   }
/*     */   
/*     */   private ZDataSource createDataSource() throws Exception {
/* 107 */     DataSource.ConnectionType ctype = this.config.getSecurity() == MailConfig.Security.SSL ? DataSource.ConnectionType.ssl : DataSource.ConnectionType.cleartext;
/*     */     
/*     */ 
/* 110 */     String id = this.localMailbox.createDataSource(new ZImapDataSource("TestImapSync", true, this.config.getHost(), this.config.getPort(), this.config.getAuthenticationId(), this.pass, "1", ctype));
/*     */     
/*     */ 
/*     */ 
/* 114 */     for (ZDataSource ds : this.localMailbox.getAllDataSources()) {
/* 115 */       if (ds.getId().equals(id)) {
/* 116 */         return ds;
/*     */       }
/*     */     }
/* 119 */     Assert.fail("Could not find data source");
/* 120 */     return null;
/*     */   }
/*     */   
/*     */   private void connect() throws IOException {
/* 124 */     this.imapConnection = new ImapConnection(this.config);
/* 125 */     this.imapConnection.connect();
/* 126 */     this.imapConnection.login(this.pass);
/*     */   }
/*     */   
/*     */   private void deleteImapFolders() throws IOException {
/* 130 */     if (this.imapConnection.isAuthenticated()) {
/* 131 */       if (this.imapFolder1.exists()) {
/* 132 */         this.imapFolder1.delete();
/*     */       }
/* 134 */       if (this.imapFolder1.exists()) {
/* 135 */         this.imapFolder1.delete();
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private class ImapFolder {
/*     */     private String name;
/*     */     
/*     */     ImapFolder(String name) {
/* 144 */       this.name = name;
/*     */     }
/*     */     
/* 147 */     String name() { return this.name; }
/*     */     
/*     */     boolean exists() throws IOException {
/* 150 */       return getListData() != null;
/*     */     }
/*     */     
/*     */     ListData getListData() throws IOException {
/* 154 */       List<ListData> lds = TestImapSync.this.imapConnection.list("", this.name);
/* 155 */       return lds.isEmpty() ? null : (ListData)lds.get(0);
/*     */     }
/*     */     
/*     */     MailboxInfo select() throws IOException {
/* 159 */       return TestImapSync.this.imapConnection.select(name());
/*     */     }
/*     */     
/*     */     void delete() throws IOException {
/* 163 */       TestImapSync.this.imapConnection.delete(name());
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestImapSync.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */